<template>
  <div class="p-2" style="display: flex; justify-content: center;">

    <el-card shadow="never" style="margin-top: 30px; width: 1000px">

      <el-table border v-loading="loading" :data="actAdjustConfigList" style="width: 1000px" class="enhanced-table">
        <el-table-column label="" align="center" prop="id" v-if="false" />
        <el-table-column label="中期调整时间范围" align="center" >
          <template #default="scope">
            <span>{{ parseTime(scope.row.startDate, "{y}-{m}-{d}") }} ~ {{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>

        <el-table-column label="二次填报开始时间" align="center" prop="twoFillData">
          <template #default="scope">
            <span>{{ parseTime(scope.row.twoFillData, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['system:actAdjustConfig:edit']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

    </el-card>
    <!-- 添加或修改中期调整时间范围配置对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" style="width: 500px" append-to-body>
      <el-form ref="actAdjustConfigFormRef" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable
                          v-model="form.startDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable
                          v-model="form.endDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="二次填报开始时间" prop="twoFillData">
          <el-date-picker clearable
                          v-model="form.twoFillData"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择二次填报时间">
          </el-date-picker>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="text-center">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ActAdjustConfig" lang="ts">
import {
  addActAdjustConfig,
  getActAdjustConfig,
  listActAdjustConfig,
  updateActAdjustConfig
} from "@/api/budget/actAdjustConfig";
import { ActAdjustConfigForm, ActAdjustConfigQuery, ActAdjustConfigVO } from "@/api/budget/actAdjustConfig/types";
import { parseTime } from "@/utils/ruoyi";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const actAdjustConfigList = ref<ActAdjustConfigVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);

const ids = ref<Array<string | number>>([]);

const total = ref(0);

const actAdjustConfigFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ""
});

const initFormData: ActAdjustConfigForm = {
  id: undefined,
  startDate: undefined,
  endDate: undefined,
  twoFillData: undefined,
};
const data = reactive<PageData<ActAdjustConfigForm, ActAdjustConfigQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    startDate: undefined,
    endDate: undefined,
    params: {}
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    startDate: [
      { required: true, message: "中期调整不能为空", trigger: "blur" }
    ],
    endDate: [
      { required: true, message: "中期调整不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询中期调整时间范围配置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listActAdjustConfig(queryParams.value);
  actAdjustConfigList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  actAdjustConfigFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};


/** 新增按钮操作 */
// const handleAdd = () => {
//   reset();
//   dialog.visible = true;
//   dialog.title = "添加配置";
// };

/** 修改按钮操作 */
const handleUpdate = async (row?: ActAdjustConfigVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getActAdjustConfig(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改配置";
};

/** 提交按钮 */
const submitForm = () => {
  actAdjustConfigFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateActAdjustConfig(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addActAdjustConfig(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
};


onMounted(() => {
  getList();
});
</script>
